package com.example.yls.inote;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.yls.inote.adapter.NotesAdapter;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private static final int REQUEST_CODE_ADD = 1;
    private RecyclerView notesListView;
    private FloatingActionButton fab;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initResources();
    }

    private void initResources() {
        notesListView = findViewById(R.id.notes_list);
        fab = (FloatingActionButton) findViewById(R.id.fab_new_notes);
        fab.setOnClickListener(this);

        notesListView.setLayoutManager(new LinearLayoutManager(this));
        notesListView.setAdapter(new NotesAdapter(this));
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fab_new_notes:
                createNewNote();
                break;
            default:
                break;
        }
    }

    private void createNewNote() {
        Intent intent = new Intent(this, NoteEditActivity.class);
        intent.putExtra("is_add", true);
        startActivityForResult(intent, REQUEST_CODE_ADD);

    }



    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        ((NotesAdapter) notesListView.getAdapter()).refresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        notesListView.setAdapter(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case REQUEST_CODE_ADD:
                resultForAddNote(resultCode, data);
                break;
        }
    }

    private void resultForAddNote(int resultCode, Intent data) {

    }
}
